package dbOperation;

import java.sql.*;

/**
 * Created by v on 17-5-18.
 */
public class DbConnection {
    //数据库仓库的相关信息
    private static String username="root";
    private static String password="he008748";
    private static String url="jdbc:mysql://119.29.190.214:3306/test";

    //连接mysql的接口对象
    private static Connection connection;

    //不允许创建这个类的其他对象
    private DbConnection(){    }

    //获取connect对象
    public static Connection getConnection(){
        if (connection==null){
            init();
        }

        return connection;
    }

    //初始化
    private static void init(){
        try {
            //初始化参量
            Class.forName("com.mysql.jdbc.Driver");
            connection= DriverManager.getConnection(url,username,password);
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    /**
     * 获取连接实例
     * @return 返回一个新的连接实例
     */
    public static Statement getStatment(){
        if (connection==null){
            init();
        }

        try {
            return connection.createStatement();
        }catch (SQLException ex){
            ex.printStackTrace();
        }

        return null;
    }
}
